<template>
  <div class="permissions-settings">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>用户权限设置</span>
          <el-button type="primary" @click="handleAddRole">添加角色</el-button>
        </div>
      </template>
      <el-table :data="roles" style="width: 100%" empty-text="暂无数据">
        <el-table-column prop="name" label="角色名称" />
        <el-table-column prop="description" label="角色描述" />
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="50%">
      <el-form :model="roleForm" label-width="120px">
        <el-form-item label="角色名称">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description" type="textarea" />
        </el-form-item>
        <el-form-item label="权限设置">
          <el-tree
            :data="permissions"
            show-checkbox
            node-key="id"
            :default-checked-keys="roleForm.permissions"
            :props="defaultProps"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSave">保存</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref } from 'vue';
import { usePermissionsStore } from '@/store/permissions';
import type { Permission } from '@/store/permissions';
import type { Role } from '@/store/roles';

export default defineComponent({
  name: 'PermissionsSettings',
  setup() {
    const permissionsStore = usePermissionsStore();
    const dialogVisible = ref(false);
    const dialogTitle = ref('添加角色');
    const roles = ref<Role[]>([]);
    const permissions = ref<Permission[]>([]);

    const roleForm = reactive<Role>({
      id: '',
      name: '',
      description: '',
      permissions: [],
      status: 'active' as 'active' | 'inactive'
    });

    const defaultProps = {
      children: 'children',
      label: 'name'
    };

    const handleAddRole = () => {
      dialogTitle.value = '添加角色';
      roleForm.id = '';
      roleForm.name = '';
      roleForm.description = '';
      roleForm.permissions = [];
      dialogVisible.value = true;
    };

    const handleEdit = (row: any) => {
      dialogTitle.value = '编辑角色';
      roleForm.id = row.id;
      roleForm.name = row.name;
      roleForm.description = row.description;
      roleForm.permissions = row.permissions;
      dialogVisible.value = true;
    };

    const handleDelete = async (row: any) => {
      await permissionsStore.deleteRole(row.id);
      await fetchRoles();
    };

    const handleSave = async () => {
      if (roleForm.id) {
        await permissionsStore.updateRole(roleForm);
      } else {
        await permissionsStore.createRole(roleForm);
      }
      dialogVisible.value = false;
      await fetchRoles();
    };

    const fetchRoles = async () => {
      //roles.value = await permissionsStore.fetchRoles();
    };

    const fetchPermissions = async () => {
      permissions.value = await permissionsStore.fetchPermissions();
    };

    fetchRoles();
    fetchPermissions();

    return {
      dialogVisible,
      dialogTitle,
      roles,
      permissions,
      roleForm,
      defaultProps,
      handleAddRole,
      handleEdit,
      handleDelete,
      handleSave
    };
  }
});
</script>

<style lang="scss" scoped>
.permissions-settings {
  padding: 20px;

  .box-card {
    margin-bottom: 20px;
  }

  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
}
</style> 